package com.heima.behavior.service.impl;

import com.alibaba.fastjson.JSON;
import com.heima.behavior.service.UserBehaviorService;
import com.heima.behavior.thread.AppThreadLocalUtil;
import com.heima.common.cache.CacheService;
import com.heima.common.constants.BehaviorConstants;
import com.heima.model.article.dtos.CollectionBehaviorDto;
import com.heima.model.behavior.dtos.LikesBehaviorDto;
import com.heima.model.behavior.dtos.ReadBehaviorDto;
import com.heima.model.behavior.dtos.UnLikesBehaviorDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class UserBehaviorServiceImpl implements UserBehaviorService {

    @Autowired
    private CacheService cacheService;

    @Override
    public ResponseResult like(LikesBehaviorDto dto) {

        if (dto == null) {
            log.warn("没有传入参数");
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        ApUser user = AppThreadLocalUtil.getUser();
        if (user == null) {
            log.warn("没有得到用户信息");
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        Integer userId = user.getId();
        if (userId == null) {
            log.warn("用户不存在");
            return ResponseResult.errorResult(AppHttpCodeEnum.AP_USER_DATA_NOT_EXIST);
        }

        Long articleId = dto.getArticleId();
        Short operation = dto.getOperation();
        if (operation == 0) {
            Object object = cacheService.hGet(articleId.toString(), userId.toString());
            if (object != null) {
                log.warn("不能重复点赞");
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
            }
            Long likeTime = System.currentTimeMillis();
            cacheService.hPut(BehaviorConstants.LIKE_BEHAVIOR + articleId,userId.toString(),likeTime.toString());
        }else {
            cacheService.hDelete(BehaviorConstants.LIKE_BEHAVIOR + articleId,userId);
        }

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult unlike(UnLikesBehaviorDto dto) {

        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE,"入参为空");
        }

        ApUser user = AppThreadLocalUtil.getUser();
        if (user == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.AP_USER_DATA_NOT_EXIST,"用户未登录");
        }

        Integer userId = user.getId();
        if (userId == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.AP_USER_DATA_NOT_EXIST,"用户不存在");
        }

        Long articleId = dto.getArticleId();
        Short type = dto.getType();
        if (type == 0) {
            Object getResult = cacheService.hGet(articleId.toString(), userId.toString());
            if (getResult != null) {
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "已标记为不喜欢");
            }
            Long currentTime = System.currentTimeMillis();
            cacheService.hPut(BehaviorConstants.UN_LIKE_BEHAVIOR + articleId, userId.toString(), currentTime.toString());
        } else if (type == 1) {
            cacheService.hDelete(BehaviorConstants.UN_LIKE_BEHAVIOR + articleId, userId.toString());
        }

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult read(ReadBehaviorDto dto) {

        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE,"入参为空");
        }

        ApUser user = AppThreadLocalUtil.getUser();
        if (user == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.AP_USER_DATA_NOT_EXIST,"用户未登录");
        }

        Integer userId = user.getId();
        if (userId == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.AP_USER_DATA_NOT_EXIST,"用户不存在");
        }

        Long articleId = dto.getArticleId();
        if (articleId == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE,"文章不存在");
        }

        String getResult = (String) cacheService.hGet(BehaviorConstants.READ_BEHAVIOR + articleId, userId.toString());
        if (getResult != null) {
            ReadBehaviorDto readBehaviorDto = JSON.parseObject(getResult, ReadBehaviorDto.class);
            Short count = readBehaviorDto.getCount();
            Short newCount = (short) (count + 1);
            dto.setCount(newCount);
        }else {
            dto.setCount((short) 1);
        }
        cacheService.hPut(BehaviorConstants.READ_BEHAVIOR + articleId,userId.toString(), JSON.toJSONString(dto));

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }




}
